package root.electronclass.electronclass.adapter;

import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import root.electronclass.electronclass.common.base.ViewBindingActivity;

import java.util.List;

public class FragmentTabAdapter implements RadioGroup.OnCheckedChangeListener {
    private List<Fragment> fragments; // 一个tab页面对应一个Fragment
    private RadioGroup rgs; // 用于切换tab
    private ViewBindingActivity fragmentActivity; // Fragment所属的Activity
    private int fragmentContentId; // Activity中所要被替换的区域的id

    private int currentTab = 0; // 当前Tab页面索引

    private OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener; // 用于让调用者在切换tab时候增加新的功能

    public FragmentTabAdapter(ViewBindingActivity fragmentActivity,
                              List<Fragment> fragments, int fragmentContentId, RadioGroup rgs, int position) {
        this.fragments = fragments;
        this.rgs = rgs;
        this.fragmentActivity = fragmentActivity;
        this.fragmentContentId = fragmentContentId;

        // 默认显示第一页
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
        Fragment fragment = fragments.get(position);
        ft.add(fragmentContentId, fragment,""+position);
//        TopKitKt.title(fragmentActivity.getBinding().top, fragment instanceof HomeFragment ? "" : "高一(13)班");//FIXME 修改为获取当前班级名字
        currentTab = position;
        ft.commit();

        rgs.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        FragmentManager fm = fragmentActivity.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        for (int i = 0; i < rgs.getChildCount(); i++) {
            if (rgs.getChildAt(i).getId() == checkedId) {
                // 如果设置了切换tab额外功能功能接口
                if (null != onRgsExtraCheckedChangedListener) {
                    onRgsExtraCheckedChangedListener.OnRgsExtraCheckedChanged(
                            radioGroup, checkedId, i);
                }

                Fragment fragment = fragments.get(i);

                ft.hide(getCurrentFragment());
                if (fragment.isAdded() && fm.findFragmentByTag(String.valueOf(i)) != null ) {
                    ft.show(fragment);
                } else {
                    ft.add(fragmentContentId, fragment,String.valueOf(i));
                }

                ft.commit();
                currentTab = i; // 更新目标tab为当前tab
            }
        }

    }

    /**
     * 切换tab
     *
     * @param idx
     */
    public void showTab(int idx) {
        FragmentManager fm = fragmentActivity.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        for (int i = 0; i < fragments.size(); i++) {
            if (idx == i) {
                Fragment fragment = fragments.get(i);
                ft.hide(getCurrentFragment());
                if (fragment.isAdded() && fm.findFragmentByTag(String.valueOf(i)) != null) {
                    ft.show(fragment);
                } else {
                    ft.add(fragmentContentId, fragment, String.valueOf(i));
                }
                ft.commit();
            }
        }
        currentTab = idx; // 更新目标tab为当前tab
    }

    /**
     * 获取一个带动画的FragmentTransaction
     *
     * @param index
     * @return
     */
    private FragmentTransaction obtainFragmentTransaction(int index) {
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager()
                .beginTransaction();
        // 设置切换动画
        // if (index > currentTab) {
        // ft.setCustomAnimations(R.anim.slide_right_in,
        // R.anim.slide_left_out);
        // } else {
        // ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_right_out);
        //
        // }
        return ft;
    }

    public int getCurrentTab() {
        return currentTab;
    }

    private Fragment getCurrentFragment() {
        return fragments.get(currentTab);
    }

    public OnRgsExtraCheckedChangedListener getOnRgsExtraCheckedChangedListener() {
        return onRgsExtraCheckedChangedListener;
    }

    public void setOnRgsExtraCheckedChangedListener(
            OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener) {
        this.onRgsExtraCheckedChangedListener = onRgsExtraCheckedChangedListener;
    }

    /**
     * 切换tab额外功能功能接口
     */
    public static class OnRgsExtraCheckedChangedListener {
       public void OnRgsExtraCheckedChanged(RadioGroup radioGroup,
                                      int checkedId, int index) {

        }
    }

}

